#include "main.h"
int main(int argc, char* argv[]){
    //cout << "sort v1.0" << endl;
    if (run(argc, argv)){return(1);}
    return 0;
}

int run(int argc, char* argv[]){
//declarations
    file fIn;
    file fOut;
    string strIn;
    string strOut;
    list lWords;

    //assign default string valuez
    strIn.assign("in.txt");
    strOut.assign("out.csv");

    //read CLI
    readCLI(argc, argv, strIn, strOut);

    //set filenames
    fIn.set(strIn);
    fOut.set(strOut);

    //set file mode
    fIn.mode = FILE_IN;
    fOut.mode = FILE_OUT;

    try{
        fIn.open();
        char cRead;
        //if file is open
        if(fIn.state()){
            bool bWord = false;
            //init string
            string strWord;
            while(!fIn.eof){
                //clear control vars
                strWord.clear();
                bWord = false;

                //loop through chars until non-alphabetic char is found
                do{
                    //get char
                    cRead = fIn.getchar();
                    //quit loop if delimiter char detected
                    if(delimiter((int) cRead)){break;}
                    //add char to string
                    strWord+=charSimplify(cRead);
                    bWord++;
                }while(1);

                //add entry to list if existent
                if(bWord){
                    //allocate space for string and termination char
                    char* zsString = new char[strWord.length()+1];
                    //copy string and termination char to memory location
                    strncpy(zsString, strWord.c_str(), strWord.length()+1);
                    //add string to list
                    lWords.add(zsString);
                }
            }
        }
        fIn.close();

        //lWords.print();
        //sort data
        lWords.sort();
        //filter double entries
        lWords.purgeclones();
        //cout << "list len: " << lWords.getLength() << endl;

        //write output file
        fOut.open();
        if(fOut.state()){
            if(DEBUG){cout << "writing to file" << endl;}
            lWords.writedata(fOut.filestr);
        }
        fOut.close();

        //ui
        //create temp vars
        string strUI; int in;
        //create ui object
        UI uiBasic;
        //reset list cursor to beginning
        lWords.resetCursor();
        //print welcome msg
        strUI.assign("Use arrow buttons to scroll, ESC to exit.\nPress ANY button to continue");
        uiBasic.update(strUI);
        //await user input/to confirm msg is read
        uiBasic.awaitinput();

        //get items
        strUI = lWords.getPrompt(0);
        while(1){
            if(strUI.length() != 0){
                uiBasic.update(strUI);
            }

            in = uiBasic.awaitinput();
            if(in == 77){
                strUI = lWords.getPrompt(1);
            }

            if(in == 75){
                strUI = lWords.getPrompt(-1);
            }
            //cout << "entered: " << dec<<in << endl;
            if(in == 27){break;}
        }

        //exit
    }catch(int e){
        cout << "exception: " << e << endl;
        return(0);
    }catch(int *str){
        cout << str << endl;
        return(0);
    }catch(...){
        cout << "unknown exception" << endl;
        return(0);
    }

    return(1);
}
int delimiter(int x){
    int val = 0;

    //increment upon alfabetic characters
    if(!(((x >= 65) && (x <= 90)) || ((x >= 97) && (x <= 122)))){val++;}

    /*switch(x){
        case 32: // space
            val++;
            break;
        default:
            break;
    }*/
    return(val);
}
